package com.leyou.auth.controller;

import com.leyou.auth.service.IauthService;
import com.leyou.common.auth.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
public class AuthCotroller {

    @Autowired
    private IauthService authService;

    @PostMapping("login")
    public ResponseEntity<Void> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {
        authService.login(username, password, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/verify")
    public ResponseEntity<UserInfo> verify(HttpServletResponse response, HttpServletRequest request) {
        return ResponseEntity.ok(authService.verify(request, response));
    }

    @PostMapping("/logout")
    public ResponseEntity<Void> logout(HttpServletResponse response, HttpServletRequest request){
        authService.logout(request,response);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/authorization")
    public ResponseEntity<String> authorize(@RequestParam("id")Long id, @RequestParam("secret")String secret){
        return ResponseEntity.ok(authService.authorize(id,secret));
    }
}
